package com.qf.administration.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.administration.entity.CarSort;
import com.qf.administration.mapper.CarSortMapper;
import com.qf.administration.qo.CarSortQO;
import com.qf.administration.service.CarSortService;
import com.qf.administration.utils.CommonBeanUtils;
import com.qf.administration.vo.CarSortVO;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @Title: CarSortServiceImpl
 * @Description:
 * @author:ZWX
 * @date: 2023/5/23 20:43
 */
@Service
public class CarSortServiceImpl implements CarSortService {
    @Resource
    private CarSortMapper carSortMapper;

    @Override
    public RespResult<String> addCarSort(CarSortQO carSortQO) {
        CarSort carSort = carSortMapper.selectById(carSortQO.getCarSortId());
        if (ObjectUtils.isEmpty(carSort)) {
            /*id不存在，则可以进行添加*/
            CarSort carSort1 = new CarSort();
            BeanUtils.copyProperties(carSortQO, carSort1);
            int insert = carSortMapper.insert(carSort1);
            if (insert > 0) {
                return RespResult.success("会议室添加成功");
            } else {
                throw new ServiceException(ResultCode.SYS_ERROR);
            }
        } else {
            /*id存在，添加失败*/
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public RespResult<String> deleteCarSort(int[] ids) {
        int count = carSortMapper.deleteByMemberId(ids);
        if (count != ids.length) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        } else {
            return RespResult.success("删除成功");
        }
    }

    @Override
    public RespResult<String> updateCarSort(CarSortQO carSortQO) {
        CarSort carSort = new CarSort();
        BeanUtils.copyProperties(carSortQO, carSort);
        int i = carSortMapper.updateById(carSort);
        if (i < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("更新成功");
    }

    @Override
    public PageInfo<CarSortVO> queryCarSort(int page, int size, String carSortName) {
        PageInfo<CarSort> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> carSortMapper.selectList(carSortName));
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return CommonBeanUtils.copyPageInfo(pageInfo, new PageInfo<>(), CarSortVO::new);
    }

    @Override
    public CarSortVO queryCarSortOne(int carSortId) {
        CarSort carSort = carSortMapper.selectById(carSortId);
        if (ObjectUtils.isEmpty(carSort)) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        } else {
            CarSortVO carSortVO = new CarSortVO();
            BeanUtils.copyProperties(carSort, carSortVO);
            return carSortVO;
        }
    }

}
